package com.liao.controller;

import com.liao.bean.Member;
import com.liao.bean.Order;
import com.liao.bean.result.Code;
import com.liao.bean.result.Msg;
import com.liao.bean.result.PageResult;
import com.liao.bean.result.Result;
import com.liao.service.LogService;
import com.liao.service.MemberService;
import com.liao.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/member")
public class MemberController {

    @Autowired
    private MemberService memberService;

    @ResponseBody
    @RequestMapping("/queryMemberListByPage")
    public PageResult<Member> queryMemberListByPage(@RequestParam(defaultValue = "1") Integer currentPage,
                                               @RequestParam(defaultValue = "5") Integer pageSize,
                                               @RequestParam(defaultValue = "") String account) throws Exception {
        PageResult<Member> pageResult = memberService.queryMemberListByPage(currentPage,pageSize,account);
        return pageResult;
    }

    @ResponseBody
    @RequestMapping("/addMember")
    public Result addMember(@RequestBody Member member) throws Exception {

        memberService.addMember(member);
        return new Result(true, Code.MEMBER_ADD_SUCCESS, Msg.MEMBER_ADD_SUCCESS);

    }

    @ResponseBody
    @RequestMapping("/editMember")
    public Result editMember( @RequestBody Member member) throws Exception {
        memberService.editMember(member);
        return new Result(true,Code.MEMBER_EDIT_SUCCESS, Msg.MEMBER_EDIT_SUCCESS);
    }

    @ResponseBody
    @RequestMapping("/deleteMember")
    public Result deleteMember(String memberId) throws Exception {
        memberService.deleteMember(memberId);
        return new Result(true,Code.MEMBER_DELETE_SUCCESS, Msg.MEMBER_DELETE_SUCCESS);
    }

    @ResponseBody
    @RequestMapping("/deleteMemberIds")
    public Result deleteMemberIds(@RequestParam("memberIds[]") List<String> memberIds) throws Exception {
        for (String memberId : memberIds) {
            try {
                memberService.deleteMember(memberId);
            }catch (Exception e){
                e.printStackTrace();
                return new Result(false,Code.MEMBER_DELETE_FAIL, Msg.MEMBER_DELETE_FAIL);
            }

        }
        return new Result(true,Code.MEMBER_DELETE_SUCCESS, Msg.MEMBER_DELETE_SUCCESS);
    }
}
